<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/17
 * Time: 17:42
 */
class MysqliDB{
    //数据库连接信息
    private $arrDbConfig = array(
        'host'    => 'localhost', //主机
        'port'    => '3306', //端口
        'username'    => '', //用户
        'password'     => '', //密码
        'charset' => 'utf8', //字符集
        'dbname'  => '', //默认数据库
    );

    //数据库连接资源句柄
    private $conn;

    //单例对象
    private static $instance;

    /**
     *私有构造刚发
     */
    private function __construct($params = array()){
        //初始化属性
        $this->initAttr($params);
        //连接数据库
        $this->connectServer();
//        //设定字符集
        $this->setCharset();
//        //选择默认数据
//        $this->selectDefaultDb();
    }

    public static function getInstance($params = array()){
        if(!self::$instance instanceof self){
            self::$instance = new self($params);
        }
        return self::$instance;
    }

    /**
     * 私有克隆
     */
    private function __clone(){}

    /**
     * 初始化属性
     * @param $params 数组
     */
    private function initAttr($params){
        $this->arrDbConfig = array_merge($this->arrDbConfig,$params);
    }

    /**
     * 连接目标数据库
     */
    private function connectServer(){
        $sHost    = $this->arrDbConfig['host'];
        $sPort    = $this->arrDbConfig['port'];
        $sUsernamer    = $this->arrDbConfig['username'];
        $sPassword     = $this->arrDbConfig['password'];
        $sDbname  = $this->arrDbConfig['dbname'];

        $this->conn = mysqli_connect($sHost,$sUsernamer,$sPassword,$sDbname);
        if(!$this->conn){
            die('连接失败:' . mysqli_connect_error);
        }
    }

    /**
     * 设定连接字符集
     */
    private function setCharset(){
        mysqli_query($this->conn,'set names' . $this->arrDbConfig['charset']);
    }

    /**
     * 选择默认数据库
     */
    private function selectDefaultDb(){
        if($this->arrDbConfig['dbname'] == ''){
            return ;
        }
        $sSql = 'use' . '`' . $this->arrDbConfig['dbname'] . '`';
        $this->query($sSql);
    }

    /**
     * @param $sSql 待执行的sql
     * @return bool|mysqli_result
     */
    public function query($sSql){
        if($result = mysqli_query($this->conn,$sSql)){
            return $result;
        }else{
            echo 'SQL执行失败:<br>';
            echo '错误的SQL为:' . $sSql . '<br>';
            echo '错误的代码为:' . mysqli_errno($this->conn) . '<br>';
            echo '错误的信息为:' . mysqli_errnr($this->conn) . '<br>';
            die;
        }
    }

    /**
     * 获取单条数据
     * @param $sSql
     * @return array|bool|null
     */
    public function getOne($sSql){
        $result = $this->query($sSql);
        if(!$result){
            return false;
        }
        return mysqli_fetch_assoc($result);
    }

    /**
     * 获取多条数据
     * @param $sSql
     * @return array|bool
     */
    public function getAll($sSql){
        $result = $this->query($sSql);
        if(!$result){
            return false;
        }
        $data = array();
        while($row = mysqli_fetch_assoc($result)){
            $data[] = $row;
        }
        return $data;
    }

}


?>